import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.omg.PortableInterceptor.Interceptor;

public class Score {
		private static Document doc;	
		private static double before=0;//课前自测部分的应得经验值总分
		private static double base=0;//课堂完成部分的应得经验值总分
		private static double test=0;//课堂小测部分的应得经验值总分
		private static double program=0;//编程题的经验值最高分
		private static double add=0;//附加题的经验值最高分
		private static double sum=0;//最后应得的成绩
		public static void main(String[] args) throws IOException {
			Properties properties=new Properties();
			InputStream src = new BufferedInputStream (new FileInputStream("..\\total.properties"));
			properties.load(src);
			Enumeration fname=properties.propertyNames();
			double SumBrfore=Integer.parseInt(properties.getProperty("before")) ;//课前自测部分
			double SumBase=Integer.parseInt(properties.getProperty("base")) ;//课堂完成部分
			double SumTest=Integer.parseInt(properties.getProperty("test")) ;//课堂小测部分
			double SumProg=Integer.parseInt(properties.getProperty("program")) ;//编程题
			double SumAdd=Integer.parseInt(properties.getProperty("add")) ;//附加题		
			File file1=new File("..\\small.html");//读取small.html
			File file2=new File("..\\all.html");//读取all.html		
			Interceptor(file1,file2,SumBrfore,SumBase,SumTest,SumProg,SumAdd);//获取html中的相对应的经验值
		}
		
		private static void Interceptor(File small_files, File all_files, double SumBrfore, double SumBase, double SumTest,
			double SumProg, double SumAdd) {
			double MyBefore=0;//课前自测部分
			double MyBase=0;//课堂完成部分
			double MyTest=0;//课堂小测部分
			double MyProg=0;//编程题
			double MyAdd=0;//附加题
			try {
				//转化为Document
				org.jsoup.nodes.Document doc1=Jsoup.parse(small_files,"utf-8");
				org.jsoup.nodes.Document doc2=Jsoup.parse(all_files,"utf-8");			
				//对small.html的相对应的经验累积
				if(doc1!=null){
					Elements elements1=doc1.getElementsByClass("interaction-row");
					double Exp;
					for(int i=0;i<elements1.size();i++) {
						if(elements1.get(i).child(1).child(2).toString().contains("已参与&nbsp")) {
							if(elements1.get(i).child(1).child(0).toString().contains("课堂完成")) {
								Scanner scanner=new Scanner(elements1.get(i).child(1).child(2).children().get(0).children().get(7).text());
								Exp=scanner.nextDouble();
								MyBase=Exp+MyBase;
							}else if(elements1.get(i).child(1).child(0).toString().contains("课堂小测")) {
								Scanner scanner=new Scanner(elements1.get(i).child(1).child(2).children().get(0).children().get(7).text());
								Exp=scanner.nextDouble();
								MyTest=Exp+MyTest;
							}else if(elements1.get(i).child(1).child(0).toString().contains("编程题")) {
								Scanner scanner=new Scanner(elements1.get(i).child(1).child(2).children().get(0).children().get(7).text());
								Exp=scanner.nextDouble();
								MyProg=Exp+MyProg;
							}else if(elements1.get(i).child(1).child(0).toString().contains("附加题")) {
								Scanner scanner=new Scanner(elements1.get(i).child(1).child(2).children().get(0).children().get(7).text());
								Exp=scanner.nextDouble();
								MyAdd=Exp+MyAdd;
							}
						} 
						else {
							if(elements1.get(i).child(1).child(0).toString().contains("课前自测")) {
								Scanner scanner=new Scanner(elements1.get(i).child(1).child(2).children().get(0).children().get(10).text());
								Exp=scanner.nextDouble();
								MyBefore=Exp+MyBefore;
							}
						}
					}
				}		
				//对all.html的相对应的经验累积
				if(doc2!=null) {
					Elements elements2=doc2.getElementsByClass("interaction-row");
					double jy1;
					for(int i=0;i<elements2.size();i++) {
						if (elements2.get(i).child(1).child(0).toString().contains("课前自测")) {
							Scanner scanner2=new Scanner(elements2.get(i).child(1).child(2).children().get(0).children().get(10).text());
							jy1=scanner2.nextDouble();
							MyBefore=jy1+MyBefore;
						}
					}				
				}
				//计算所得分
				before=MyBefore/SumBrfore*100*0.25;
				base=MyBase/SumBase*100*0.95*0.3;
				test=MyTest/SumTest*100*0.2;
				program=MyProg/SumProg*100*0.1;
				add=MyAdd/SumAdd*100*0.05;
				sum=(base+before+test+program+add)*0.9+5;
				System.out.println("before:"+before);
				System.out.println("base:"+base);
				System.out.println("test:"+test);
				System.out.println("program:"+program);
				System.out.println("add:"+add);
				System.out.println(String.format("%.2f",sum));
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}	
		}
}